package com.lz.yz.homepage.service.impl;

import com.github.pagehelper.PageHelper;
import com.lz.yz.base.entity.ReturnValue;
import com.lz.yz.home.dao.BannerMapper;
import com.lz.yz.home.entity.Banner;
import com.lz.yz.home.param.HomePageParam;
import com.lz.yz.homepage.service.BannerService;
import com.util.common.Page;
import com.util.common.QueryList;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * Created by Administrator on 2017/3/24 0024.
 */
@Service
public class BannerServiceImpl implements BannerService {

    @Autowired
    BannerMapper bannerMapper;

    @Override
    public ReturnValue listBanners(HomePageParam param) {
        PageHelper.startPage(param.getPage(), param.getPageSize());
        List<Banner> list = bannerMapper.listBanners(param);
        QueryList<Banner> queryList = new QueryList(list, Page.toPage(list));
        return ReturnValue.generateTrueReturnValue(queryList);
    }

    @Override
    public Banner getOne(Integer id) {
        return bannerMapper.selectByPrimaryKey(id);
    }

    @Override
    public ReturnValue getList(HomePageParam param) {
        PageHelper.startPage(param.getPage(), param.getPageSize());
        List<Banner> list = bannerMapper.listBanners(param);
        QueryList<Banner> queryList = new QueryList(list, Page.toPage(list));
        return ReturnValue.generateTrueReturnValue(queryList);
    }

    @Override
    public void delete(Integer id) {
        bannerMapper.del(id);
    }

    @Override
    public void addBanner(Banner model) {
        bannerMapper.insert(model);
//        if (model.getBannerId() == null){//此时新增
//            bannerMapper.insert(banner);
//        }else{
//            //更新
//            banner.setBannerId(model.getBannerId());
//            bannerMapper.updateByPrimaryKey(banner);
//        }
    }

    @Override
    public void update(Banner model) {
        Banner banner = bannerMapper.selectByPrimaryKey(model.getId());
        BeanUtils.copyProperties(model,banner);
        bannerMapper.updateByPrimaryKey(banner);
    }
}
